package org.jeecg.modules.wlhy.hyplatform.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.channel.bankdocking.dto.Acc;
import org.jeecg.modules.paycenter.channel.bankdocking.dto.RespPacket;
import org.jeecg.modules.paycenter.channel.bankdocking.utils.BankUtils;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyplatform.entity.HyPlatformAccountBook;
import org.jeecg.modules.wlhy.hyplatform.mapper.HyPlatformAccountBookMapper;
import org.jeecg.modules.wlhy.hyplatform.service.IHyPlatformAccountBookService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @Description: hy_platform_account_book
 * @Author: jeecg-boot
 * @Date:   2021-08-28
 * @Version: V1.0
 */
@Service
public class HyPlatformAccountBookServiceImpl extends ServiceImpl<HyPlatformAccountBookMapper, HyPlatformAccountBook> implements IHyPlatformAccountBookService {

    @Autowired
    private BankUtils bankUtils;

    @Autowired
    private IHySettingService settingService;

    @Override
    public HyPlatformAccountBook getAccountBookInfo() {

        HyPlatformAccountBook accountBook = this.getById("1");
        if(accountBook != null){
            Acc customerAcc = this.getAcc(accountBook);
            if(customerAcc != null){
                BigDecimal amt = customerAcc.getAvailBal();
                accountBook.setAvailAmt(amt);
                accountBook.setAccSts(customerAcc.getAccSts());
            }else {
                accountBook.setAvailAmt(BigDecimal.ZERO);
                accountBook.setAccSts("-99");
            }
        }
        return accountBook;
    }

    @Override
    public Acc getAcc(HyPlatformAccountBook accountBook) {
        try {
            RespPacket packet = bankUtils.getAccountBookBalance(accountBook.getAccNo(), accountBook.getProv(), accountBook.getLogAccNo());
            if(packet.isSuccess()){
                return packet.getAcc();
            }
        }catch (Exception e){

        }
        return null;
    }

    @Override
    @Transactional
    public void addAccountBook() {
        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if(!"1".equals(settingValue)) {
            throw new JeecgBootException("系统暂时关闭支付功能");
        }

        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getNhbankIzUse()){
            throw new JeecgBootException("未开通服务");
        }
        String nhbankAccName = config.getNhbankAccName();
        if(StringUtils.isEmpty(nhbankAccName)){
            throw new JeecgBootException("未配置农行账户名称！");
        }

        HyPlatformAccountBook accountBook = this.getById("1");
        if(accountBook != null){
            throw new JeecgBootException("不能重复开通");
        }
        String logAccName = nhbankAccName;
        String logAccNo =  "5" + RandomUtil.randomNumbers(9);
        accountBook = new HyPlatformAccountBook();
        accountBook.setId("1");
        accountBook.setAccNo(config.getNhbankAccno());
        accountBook.setProv(config.getNhbankProv());
        accountBook.setLogAccName(logAccName);
        accountBook.setLogAccNo(logAccNo);
        if( this.save(accountBook)){
            try {
                RespPacket packet = bankUtils.AddAccountBook(logAccName, logAccNo);
                if(!packet.isSuccess()){
                    throw new JeecgBootException(packet.getRespInfo());
                }
            } catch (Exception e) {
                throw new JeecgBootException(e.getMessage());
            }
        }else {
            throw new JeecgBootException("添加失败");
        }
    }

    @Override
    public HyPlatformAccountBook getAccountBook() {
        HyPlatformAccountBook accountBook = this.getById("1");
        return accountBook;
    }

    @Override
    @Transactional
    public void updatePlateformAccInfo(HyPlatformAccountBook platformAccountBook) throws Exception {
        RespPacket respPacket = bankUtils.editAccountBookName(platformAccountBook.getLogAccNo(), platformAccountBook.getLogAccName());
        if(respPacket.isSuccess()){
            this.updateById(platformAccountBook);
            return;
        }
        throw new Exception(respPacket.getRespInfo());
    }
}
